* Begin by preparing/formatting all datafiles:
	* Calcbench data for PPE
	* Calcbench data for pensions
	* Calcbench data for tax credits (amount)
	* Calcbench data for tax credits (percentage)
	* Calcbench data for pretax foreign earnings (mostly for banks/financial)
	
* Prep calcbench data (DTL for PPE):
clear
capture use "DIRECTORY\calcbench_ppe"

	* Standardize naming conventions so that they can be matched to Compustat
	rename cik_code cik 
	rename ticker tic 
	rename fiscal_year fyear 

keep cik tic fyear value 
duplicates drop cik tic fyear value, force 
* Delete observations where we have different values for the same variable
	duplicates tag cik tic fyear,gen(X)
	drop if X > 0 
	* Delete negative values	
	drop if value < 0 
save temp_ppe, replace 

* Prep calcbench data (DB Plans):
clear
capture import delim "DIRECTORY\calcbench_data_pension.csv"
	* Standardize naming conventions so that they can be matched to Compustat
	rename ticker tic 
	rename value dta_benefit 
	* Put amounts in millions USD
	replace dta_benefit = dta_benefit/1000000
	* Get fiscal year from date
	gen fyear = substr(fiscal_period,1,4)
	destring fyear,replace
	
keep cik tic fyear dta_benefit 
duplicates drop cik tic fyear dta_benefit, force 

* Delete observations where we have different values for the same variable
	duplicates tag cik tic fyear,gen(X)
	drop if X > 0 
	
rename dta_benefit benefits_dta
save temp_benefits, replace 

* Prep calcbench data (tax credits on the ETR statement):
clear
capture use "DIRECTORY\Data\etr_credits"
* Standardize naming so that it can be matched to Compustat
	rename cik_code cik 
	rename ticker tic 
	rename fiscal_year fyear 

keep cik tic fyear value 
rename value etrcredits
duplicates drop cik tic fyear etrcredits, force 

* Delete observations where we have different values for the same variable
	duplicates tag cik tic fyear,gen(X)
	drop if X > 0 
	
save temp_taxcredits, replace 

* Prep calcbench data (tax credit percentage on the ETR statement):
clear
capture use "DIRECTORY\etr_credit_pct"
* Standardize naming so that it can be matched to Compustat
	rename cik_code cik 
	rename ticker tic 
	rename fiscal_year fyear 

keep cik tic fyear value 
rename value credit_pct
duplicates drop cik tic fyear credit_pct, force 

* Delete observations where we have different values for the same variable
	duplicates tag cik tic fyear,gen(X)
	drop if X > 0 

save temp_taxcredit_pct, replace 

* Prep calcbench data - pretax foreign income:
clear 
capture use "DIRECTORY\pifo"
keep if metric == "EBITForeign"
* Standardize naming so that it can be matched to Compustat
	rename cik_code cik 
	rename ticker tic 
	rename fiscal_year fyear 
keep cik tic fyear value 
rename value pifo_1 
duplicates drop cik tic fyear pifo_1, force

* Delete observations where we have different values for the same variable
	duplicates tag cik tic fyear,gen(X) 
	drop if X > 0 
	drop X 
	
destring cik, replace 
save temp_pifo, replace 

* Modify Compustat data:
clear 
capture use "DIRECTORY\compustat_data_2009-2023.dta"
cd "DIRECTORY"

* Drop if missing key variables: 
	drop if missing(pi)
	drop if missing(txpd)
	duplicates tag gvkey fyear,gen(x0)
	drop if x0 > 0 & indfmt == "FS"

	* Merge with Calcbench Data:
		duplicates tag cik tic fyear ,gen(X)
		drop if X > 0 
		drop X
		merge 1:1 cik tic fyear using temp_ppe  
		drop if _merge == 2
		drop _merge 
		merge 1:1 cik tic fyear using temp_taxcredits
		drop if _merge == 2
		drop _merge 
		merge 1:1 cik tic fyear using temp_taxcredit_pct
		drop if _merge == 2
		drop _merge 
		destring cik, replace
		merge 1:1 cik tic fyear using temp_benefits
		drop if _merge == 2
		drop _merge 
		merge 1:1 cik tic fyear using temp_pifo 
		drop if _merge == 2 
		drop _merge 
		replace pifo_1 = pifo_1/1000000
		replace pifo = pifo_1 if missing(pifo)
		
		* For Calcbench data not already in millions USD, we convert here:
		gen dtl = value/1000000
		replace etrcredits = etrcredits/1000000
		
* Adjust fiscal year for tax year:
	gen acct_end  = 0
	gen month = month(datadate)
	gen year = year(datadate)
	gen day = day(datadate)
	gen double acct_end_datadate = year * 10000+ month * 100 + day

	gen tax_year = .
	replace tax_year = 2008 if acct_end_datadate >= 20080701 & acct_end_datadate <= 20090630 
	replace tax_year = 2009 if acct_end_datadate >= 20090701 & acct_end_datadate <= 20100630 
	replace tax_year = 2010 if acct_end_datadate >= 20100701 & acct_end_datadate <= 20110630 
	replace tax_year = 2011 if acct_end_datadate >= 20110701 & acct_end_datadate <= 20120630 
	replace tax_year = 2012 if acct_end_datadate >= 20120701 & acct_end_datadate <= 20130630 
	replace tax_year = 2013 if acct_end_datadate >= 20130701 & acct_end_datadate <= 20140630 
	replace tax_year = 2014 if acct_end_datadate >= 20140701 & acct_end_datadate <= 20150630 
	replace tax_year = 2015 if acct_end_datadate >= 20150701 & acct_end_datadate <= 20160630 
	replace tax_year = 2016 if acct_end_datadate >= 20160701 & acct_end_datadate <= 20170630 
	replace tax_year = 2017 if acct_end_datadate >= 20170701 & acct_end_datadate <= 20180630 
	replace tax_year = 2018 if acct_end_datadate >= 20180701 & acct_end_datadate <= 20190630 
	replace tax_year = 2019 if acct_end_datadate >= 20190701 & acct_end_datadate <= 20200630
	replace tax_year = 2020 if acct_end_datadate >= 20200701 & acct_end_datadate <= 20210630 
	replace tax_year = 2021 if acct_end_datadate >= 20210701 & acct_end_datadate <= 20220630 
	replace tax_year = 2022 if acct_end_datadate >= 20220701 & acct_end_datadate <= 20230630 
	replace tax_year = 2023 if acct_end_datadate >= 20230701 & acct_end_datadate <= 20240630  
	drop year
	gen oldfyear = fyear
	replace fyear = tax_year
		egen gvkeyid = group(gvkey)
		duplicates tag gvkey fyear,gen(Xx)
		drop if Xx > 0 
		drop Xx
		xtset gvkeyid fyear 
	drop if fyear < 2010
* Drop if ticker contains a number (evidence of being a subsidiary)
	gen drop = 0 
	forvalues i = 1/9 {
		replace drop = 1 if strpos(tic,"`i'") > 0
	}
	drop if drop == 1 
	
/*
Try to estimate federal taxes paid by back SALT and foreign taxes out of cash tax paid
*/
	
	gen cash_etr = txpd/pi 
	gen etr = txt/pi 
	gen fed_tax = txpd - txs - txfo  
		replace fed_tax = txpd - txfo if missing(txs) & missing(fed_tax)
		replace fed_tax = txpd - txfo if missing(txs) & missing(fed_tax)
		replace fed_tax = txpd - txs if missing(fed_tax)
		replace fed_tax = txpd if missing(txs) & missing(fed_tax)
		replace fed_tax = txpd if missing(fed_tax)
		replace fed_tax = txfed + txdfed if missing(txpd) & missing(fed_tax)
		replace fed_tax = 0 if fed_tax < 0 
				
	* Try to find federal deferred taxes:	
	replace txdfed = txdi - txdfo - txds if txdfo != . & txds != . & txdfed == . & txt == txc + txdi		
	replace txdfed = txdi - txdfo if txdfo != . & txdfed == . & txt == txc + txdi
	* Try to find federal current taxes:	
	replace txfed = txc - txfo - txs if txfo != . & txs != . & txfed == . & txt == txc + txdi		

	drop if missing(fed_tax)	

* Create Table 1:
	* Save the file as a temp file so that we can construct table 1, which pulls out 
	* firms that would not be subject to CAMT and non-US firms
 
	save temp, replace
	
* Keep only if PI exceeds $1B USD:	
	keep if pi > 1000 & pi != .
	keep if fyear == 2021 | fyear == 2022 | fyear == 2020
	drop if loc != "USA"
	drop if fic != "USA"
* Drop non-C corps:
	capture drop X 
	drop if sic == "6798"
	gen X = 1 if strpos(conm,"TRUST")>0
		replace X = 1 if strpos(conm,"LLC")>0
		
	* Drop if 6-digit cusip ends in Y or Z
	gen cusip_6 = substr(cusip, 1,6)
	gen cusip_drop = substr(cusip_6,-1,.)
		replace X = 1 if cusip_drop == "Y" 
		replace X = 1 if cusip_drop == "Z" 
			
	* Drop if last two letters of conm strpos(conm, "LP")> 0
	gen var = substr(conm, -3,.)
		replace X = 1 if strpos(conm, "LLP")> 0 
		replace X = 1 if var == " LP" 	
	
	drop if X ==1
	
	label var at "Total Assets"
	label var lt "Total Liabilities"
	label var ni "Net Income"
	label var sale "Sales"
	label var pi "Pretax Income"
	label var pidom "Pretax Domestic Income"
	label var pifo "Pretax Foreign Income"
	label var txfed "Federal Income Tax"
	label var txdfed "Deferred Federal Income Tax"
	label var txfo "Foreign Income Tax"
	label var txdfo "Foreign Deferred Income Tax"
	label var txpd "Cash Taxes Paid"
	label var txs "State and Local Income Taxes"

* This creates Panel A of Table 1:
estpost sum at lt ni sale pi pidom pifo txfed txdfed txfo txdfo txpd txs etr cash_etr , detail 
	esttab, cells("count p5 p25 p50 p75 p95 mean sd") noobs sfmt(%12.2fc) 
	eststo clear 

* Return to the saved file and calculate AFSI for firms so that we can create 
* Table 1 panel B, which is restricted to firms that are applicable corporations:
	use temp ,clear

forvalues i = 2010/2022 {
	gen afsi_`i'_0 = pi if pi > 1000 & fyear == `i' 
	gen tax_paid_`i'_0 = fed_tax if fyear == `i' 
	replace tax_paid_`i'_0 = 0 if tax_paid_`i'_0 < 0
	gen tent_camt_`i'_0 = afsi_`i'_0 * .15 
	
	gen xcamt_liab_`i'_0 = tent_camt_`i'_0 - tax_paid_`i'_0
		replace xcamt_liab_`i'_0 = 0 if xcamt_liab_`i'_0 < 0
		replace xcamt_liab_`i'_0 = 0 if fyear != `i'
		replace xcamt_liab_`i'_0 = 0 if missing(xcamt_liab_`i'_0)	
	egen camt_liab_`i'_0 = sum(xcamt_liab_`i'_0),by(gvkey)
	}	
	
	drop rank	
	
* Adjustments to AFSI:
	* SALT:
		replace txs = 0 if missing(txs)
	* Depreciation: 
		* Calculate change in the DTL and gross up the amount by the tax rate
		* to estimate tax depreciation related to PPE
		gen l_dtl = L.dtl 
		gen change_dtl = dtl - l_dtl 
		gen gross_up = change_dtl/.21 if fyear > 2017
			replace gross_up = change_dtl/.35 if fyear <= 2017
		* Because not all firms report this amount, estimate PPE based on median:
		gen capex_gross = gross_up/capx 
		egen median_ratio = median(capex_gross)
		gen adj_to_dep = gross_up 
		gen imputed_dep_adj = 0
			replace imputed_dep_adj = 1 if missing(adj_to_dep) & capx != .
		replace adj_to_dep = median_ratio * capx if missing(adj_to_dep)	
		replace dp = dpc if missing(dp)
		* Calculate tax depreciation by adding/subtracting book-tax difference 
		* to/from book tax
		gen tax_dep = dp + adj_to_dep
		replace tax_dep = 0 if missing(tax_dep)
		gen og_dep = dp 
		replace dp = 0 if tax_dep == 0 
	* Pension Adjustment:
		* Caclulate change in the DTA and then gross up by the tax rate
		gen l_dta = L.benefits_dta 
		gen change_dta = benefits_dta - l_dta 
		gen gross_up_dta = change_dta/.21 if fyear > 2017
			replace gross_up = change_dta/.35 if fyear <= 2017
		replace gross_up_dta = 0 if missing(gross_up_dta)
		
	* Adjust AFSI for Verizon, AT&T, T-Mobile, and Dish
		gen spectrum = 0
		replace spectrum = 4380 if tic == "VZ"
		replace spectrum = 3820 if tic == "T"
		replace spectrum = 930 if tic == "TMUS"
		replace spectrum = 1200 if tic == "DISH"	
	* Adjust for insurance gains/losses
		* These are the SIC codes for insurance companies
		gen ins_co = 0
		replace ins_co = 1 if sic == "6311"
		replace ins_co = 1 if sic == "6321"
		replace ins_co = 1 if sic == "6324"
		replace ins_co = 1 if sic == "6331"
		replace ins_co = 1 if sic == "6351"
		replace ins_co = 1 if sic == "6361"
		replace ins_co = 1 if sic == "6399"
		replace ins_co = 1 if sic == "6411"
		gen ins_gain = gliv if ins_co == 1 
		replace ins_gain = 0 if missing(ins_gain)		
* Determine firms in scope:	 
	gen afsix =  pi - txs  + dp - tax_dep  + gross_up_dta - spectrum - ins_gain
	forvalues i = 2010/2022 {
		* This variable is used to determine the look back period of the 
		* applicable corporation test.
		global x = `i' -4
		* Identify the AFSI that can be used in the applicable corporation test 
		gen test_`i' = afsix if fyear < `i' & fyear > $x
		* Calculate average AFSI for the lookback period in each year
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000 & fyear == `i'
		replace app_corp_`i' = 0 if missing(app_`i') 
		egen xapp = sum(app_corp_`i'),by(gvkey)
		drop app_corp_`i' 
		rename xapp app_corp_`i'
	}	
* We overwrite the saved file so that we have the baseline AFSI that firms
* will end up with. This allows us to keep track of firms' applicable 
* corporation status as the sample is adjusted

save temp, replace
	* Panel B summary table
	keep if fyear == 2021 | fyear == 2022 | fyear == 2020
	* We again drop non-C corporations and non-US firms.
	drop if loc != "USA"
	drop if fic != "USA"
	capture drop X 
	drop if sic == "6798"
	gen X = 1 if strpos(conm,"TRUST")>0
		replace X = 1 if strpos(conm,"LLC")>0
		
	* Drop if 6-digit cusip ends in Y or Z
	gen cusip_6 = substr(cusip, 1,6)
	gen cusip_drop = substr(cusip_6,-1,.)
		replace X = 1 if cusip_drop == "Y" 
		replace X = 1 if cusip_drop == "Z" 
			
	* Drop if last two letters of conm strpos(conm, "LP")> 0
	gen var = substr(conm, -3,.)
		replace X = 1 if strpos(conm, "LLP")> 0 
		replace X = 1 if var == " LP" 	

	drop if X ==1
	keep if app_corp_2022 == 1
	
	label var at "Total Assets"
	label var lt "Total Liabilities"
	label var ni "Net Income"
	label var sale "Sales"
	label var pi "Pretax Income"
	label var pidom "Pretax Domestic Income"
	label var pifo "Pretax Foreign Income"
	label var txfed "Federal Income Tax"
	label var txdfed "Deferred Federal Income Tax"
	label var txfo "Foreign Income Tax"
	label var txdfo "Foreign Deferred Income Tax"
	label var txpd "Cash Taxes Paid"
	label var txs "State and Local Income Taxes"
* This generates the summary statistics of Table 1 Panel B:	
estpost sum at lt ni sale pi pidom pifo txfed txdfed txfo txdfo txpd txs etr cash_etr , detail 
	esttab, cells("count p5 p25 p50 p75 p95 mean sd") noobs sfmt(%12.2fc) 
	eststo clear
	

	* Calculate stats for this:
		use temp, clear
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.
		forvalues i = 2010/2022 {
			gen x_`i' = 0
			replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
			egen n_`i' = sum(x_`i')
			gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
			gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
			egen etr_`i' = mean(x_etr_`i')
			egen cash_etr_`i' = mean(x_cash_etr_`i')
		}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year and the number 
		forvalues i = 2010/2022 {
			egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
			rename total_camt_`i'_0 camt_`i'
		}
		gen note = "01. Pretax income over $1 billion"
		keep note camt_*  n_* etr_* cash_etr_*
		duplicates drop note, force 
		save camt_est, replace 
	* Find the firms which expect to pay the most in each year:	
		forvalues i = 2010/2022{
			use temp, clear 
			duplicates drop gvkey, force 
			gsort -camt_liab_`i'_0 
			rename camt_liab_`i'_0 camt_liab_`i'
			gen rank = _n 
			keep if rank < 31
			keep conm camt_liab_`i' rank
			gen note = "01. Pretax income over $1 billion"
			save camt_firms_`i', replace 
			* This allows us to track which firms enter and exit the regime and why			
			use temp, clear 
			keep if camt_liab_`i'_0 > 0 & fyear == `i'
			keep if fyear == `i'
			keep gvkey conm 
			duplicates drop gvkey, force 
			save L_`i', replace
		}
		
		
* Drop Non-US Companies:
	use temp, clear
	forvalues i = 2010/2022 {
		gen xdrop_`i' = 0
		replace xdrop_`i' = 1 if loc != "USA" & fyear == `i'
		replace xdrop_`i' = 1 if fic != "USA" & fyear == `i'		
		egen drop_`i' = sum(xdrop_`i'),by(gvkey)
		* Replace CAMT liability/App Corp status with zero if the firm is not US 
		replace camt_liab_`i'_0 = 0 if drop_`i' > 0 
	}	
	
	save temp, replace
	
	* Calculate stats for this:
	* We repeat the process to find the number of firms paying CAMT in each year
	*and their respective book and cash ETRs in that year
	forvalues i = 2010/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
	* This calculates aggregate CAMT liability by year 
	
	forvalues i = 2010/2022 {
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
		
	}
		gen note = "02. Drop non-US companies"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "02. Drop non-US companies"
		drop _merge 
		save CAMT_2022, replace 
	forvalues i = 2010/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "02. Drop non-US companies"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "02. Drop non-US companies"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 

	
	
* Drop REITs, LLPs, and Trusts:
	use temp, clear
	* Drop REITs, LLPs, Trusts
	forvalues i = 2010/2022 {
		replace xdrop_`i' = 1 if sic == "6798"
		replace xdrop_`i' = 1 if strpos(conm,"TRUST")>0
		replace xdrop_`i' = 1 if strpos(conm,"LLC")>0
		drop drop_*
		egen drop_`i' = sum(xdrop_`i'),by(gvkey)
	}
			
	* Drop if 6-digit cusip ends in Y or Z
	gen cusip_6 = substr(cusip, 1,6)
	gen cusip_drop = substr(cusip_6,-1,.)
	forvalues i = 2010/2022 {
		replace xdrop_`i' = 1 if cusip_drop == "Y" & fyear == `i'
		replace xdrop_`i' = 1 if cusip_drop == "Z" & fyear == `i'
	}
			
	* Drop if last two letters of conm strpos(conm, "LP")> 0
	gen var = substr(conm, -3,.)
	forvalues i = 2010/2022 {
		replace xdrop_`i' = 1 if strpos(conm, "LLP")> 0 & fyear == `i'
		replace xdrop_`i' = 1 if var == " LP" & fyear == `i'	
		capture drop drop_`i'
		egen drop_`i' = sum(xdrop_`i'),by(gvkey)
		replace camt_liab_`i'_0 = 0 if drop_`i' > 1
	}
			
	save temp, replace
	
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
		forvalues i = 2010/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
	forvalues i = 2010/2022 {
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
	}
		gen note = "03. Drop REITs, LLPs, LLCs, and trusts"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "03. Drop REITs, LLPs, LLCs, and trusts"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace 		
	forvalues i = 2010/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "03. Drop REITs, LLPs, LLCs, and trusts"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "03. Drop REITs, LLPs, LLCs, and trusts"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
	
* Step 3: Adjustment for SALT:
	use temp, clear
*  ORIGINAL PARAMETERS:	
	* Assume SALT is zero if missing current SALT
		replace txs = 0 if missing(txs)
	* Deduct SALT from pretax income:
	gen afsi = pi - txs 
	* Drop previously calculated liabilities/tentative taxes
	drop afsi_* tax_paid_* tent_camt_* xcamt_* camt_liab* 
	
	forvalues i = 2010/2022{
		* Calculate annual AFSI
		gen afsi_`i'_0 = pi - txs if fyear == `i' & pi > 0 
		* If AFSI does not exceed $1B, replace with zero
		replace afsi_`i'_0 = 0 if afsi_`i'_0 < 1000		
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_0 = fed_tax if fyear == `i' 
		replace tax_paid_`i'_0 = 0 if tax_paid_`i'_0 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_0 = afsi_`i'_0 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_0 = tent_camt_`i'_0 - tax_paid_`i'_0
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_0 = 0 if xcamt_liab_`i'_0 < 0
		replace xcamt_liab_`i'_0 = 0 if fyear != `i'
		egen camt_liab_`i'_0 = sum(xcamt_liab_`i'_0),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_0 = 0 if drop_`i' > 0 
	}
	save temp, replace	
	
	
	* Calculate stats for this:
	forvalues i = 2010/2022 {
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
		forvalues i = 2010/2022 {
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
	}
		gen note = "04. Adjustment for state and local taxes"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "04. Adjustment for state and local taxes"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace 
	forvalues i = 2010/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "04. Adjustment for state and local taxes"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "04. Adjustment for state and local taxes"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
		
* Step 5: Apply applicable corporation rule. Average AFSI must exceed 1B on average across prior three years:
	use temp, clear
	
	* Determine whether the firms is an applicable corporation
	forvalues i = 2010/2022 {
	* Identify the amount of time that exists to create the lookback window
	global x = `i' -4
	* Tested income is equal to AFSI if the year is during that year's lookback
	* window.
	gen test_`i' = afsi if fyear < `i' & fyear > $x
	* App. Corp. Test is based on average AFSI, so take the average of the window
	egen app_`i' = mean(test_`i'),by(gvkey)
	gen app_corp_`i' = 0
		* If greater than $1B, it is an app corp. 
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
	replace camt_liab_`i'_0 = 0 if app_corp_`i' == 0 
	}
	
	save temp, replace 
	* Calculate stats for this:
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
	forvalues i = 2013/2022 {
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
	}
		gen note = "05. AFSI must exceed $1B on average for prior 3 FYs"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "05. AFSI must exceed $1B on average for prior 3 FYs"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace 
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "05. AFSI must exceed $1B on average for prior 3 FYs"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "05. AFSI must exceed $1B on average for prior 3 FYs"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
			
* Step 6: Adjusted net income for tax depreciation:
	use temp, clear
	
	* Depreciation adjustment:
		sort gvkeyid fyear
		xtset gvkeyid fyear 
	* Add book depreciation back to AFSI and deduct estimated tax depreciation
		* Note: tax_dep was calculated in an earlier step. See above for explanation
		gen l_dtl = L.dtl 
		gen change_dtl = dtl - l_dtl 
		gen gross_up = change_dtl/.21 if fyear > 2017
			replace gross_up = change_dtl/.35 if fyear <= 2017
		gen capex_gross = gross_up/capx 
		egen median_ratio = median(capex_gross)
		gen adj_to_dep = gross_up 
		gen imputed_dep_adj = 0
			replace imputed_dep_adj = 1 if missing(adj_to_dep) & capx != .
		replace adj_to_dep = median_ratio * capx if missing(adj_to_dep)	
		replace dp = dpc if missing(dp)
		gen tax_dep = dp + adj_to_dep
		replace tax_dep = 0 if missing(tax_dep)
		gen og_dep = dp 
		replace dp = 0 if tax_dep == 0 
		
	replace afsi = pi - txs + dp - tax_dep 
	forvalues i = 2013/2022 {
		* Calculate annual AFSI
		gen afsi_`i'_2 = pi - txs + dp - tax_dep if fyear == `i' & pi > 0 
		replace afsi_`i'_2 = 0 if afsi_`i'_2 < 0
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_2 = fed_tax if fyear == `i'
		replace tax_paid_`i'_2 = 0 if tax_paid_`i'_2 < 0	
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_2 = afsi_`i'_2 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_2 = tent_camt_`i'_2 - tax_paid_`i'_2
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_2 = 0 if xcamt_liab_`i'_2 < 0
		egen camt_liab_`i'_2 = sum(xcamt_liab_`i'_2),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_2 = 0 if drop_`i' > 0				
	}	

	drop test* app_* 
	drop *_0
	* Applicable corporation test:(see prior step for details)
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsi if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
			replace app_corp_`i' = 1 if app_`i' > 1000
			replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_2 = 0 if app_corp_`i' == 0 
		rename camt_liab_`i'_2 camt_liab_`i'_0 
	}		
	save temp, replace
	
	* Calculate stats for this:
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
	forvalues i = 2013/2022 {
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
	}
		gen note = "06. Adjustment for tax depreciation"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "06. Adjustment for tax depreciation"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "06. Adjustment for tax depreciation"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "06. Adjustment for tax depreciation"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
			
* Step 7: Pension DTA adjustment:
	use temp, clear
	* See earlier step for explanation for defining benefit-adjustment
	gen l_dta = L.benefits_dta 
	gen change_dta = benefits_dta - l_dta 
	gen gross_up_dta = change_dta/.21 if fyear > 2017
		replace gross_up = change_dta/.35 if fyear <= 2017
	replace gross_up_dta = 0 if missing(gross_up_dta)
	
	drop afsi_20* tax_paid_* tent_cam* xcamt* camt_* 
	
	forvalues i = 2013/2022 {
		* Calculate annual AFSI - incorporate DB pension adj. (gross_up_dta)
		gen afsi_`i'_2 = pi - txs + dp - tax_dep + gross_up_dta if fyear == `i' & pi > 0
		replace afsi_`i'_2 = 0 if afsi_`i'_2 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_2 = fed_tax if fyear == `i'
		replace tax_paid_`i'_2 = 0 if tax_paid_`i'_2 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_2 = afsi_`i'_2 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_2 = tent_camt_`i'_2 - tax_paid_`i'_2
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_2 = 0 if xcamt_liab_`i'_2 < 0
		egen camt_liab_`i'_2 = sum(xcamt_liab_`i'_2),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_2 = 0 if drop_`i' > 0
	}
	drop test* app_* 
	replace afsi =  pi - txs + dp - tax_dep + gross_up_dta
	* Applicable corporation test:
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsi if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
			replace app_corp_`i' = 1 if app_`i' > 1000
			replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_2 = 0 if app_corp_`i' == 0 
		rename camt_liab_`i'_2 camt_liab_`i'_0 
	}			
	save temp, replace		
	
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
	forvalues i = 2013/2022 {
		* This calculates aggregate CAMT liability in each year 
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
	}
		gen note = "07. Pension adjustment"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "07. Pension adjustment"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "07. Pension adjustment"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "07. Pension adjustment"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
		
* Step 8: Adjust for Spectrum:
	use temp, clear
	* Adjust AFSI for Verizon, AT&T, T-Mobile, and Dish
		gen spectrum = 0
		replace spectrum = 4380 if tic == "VZ"
		replace spectrum = 3820 if tic == "T"
		replace spectrum = 930 if tic == "TMUS"
		replace spectrum = 1200 if tic == "DISH"
	drop tax_paid* tent_cam* xcamt* camt_liab* 	
	forvalues i = 2013/2022 {
		* Calculate annual AFSI - adding in consideration of spectrum 
		* amortization. This amount was determined in an earlier step 
		replace afsi_`i'_2 = afsi_`i'_2 - spectrum
		replace afsi_`i'_2 = 0 if afsi_`i'_2 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_2 = fed_tax if fyear == `i' 
		replace tax_paid_`i'_2 = 0 if tax_paid_`i'_2 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_2 = afsi_`i'_2 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_2 = tent_camt_`i'_2 - tax_paid_`i'_2
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_2 = 0 if xcamt_liab_`i'_2 < 0
		egen camt_liab_`i'_2 = sum(xcamt_liab_`i'_2),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_2 = 0 if drop_`i' >0
	}
	drop test* app_* 
	* Applicable corporation test:
	replace afsi =  pi - txs + dp - tax_dep + gross_up_dta - spectrum
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsi if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
			replace app_corp_`i' = 1 if app_`i' > 1000
			replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_2 = 0 if app_corp_`i' == 0 
		rename camt_liab_`i'_2 camt_liab_`i'_0 
	}					
	save temp, replace		
	
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
	forvalues i = 2013/2022 {
		* This calculates aggregate CAMT liability in each year
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
	}
		gen note = "08. Deduct spectrum amortization from AFSI"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "08. Deduct spectrum amortization from AFSI"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "08. Deduct spectrum amortization from AFSI"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "08. Deduct spectrum amortization from AFSI"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
	
* Remove unrealized gains and losses for insurance companies:
	use temp, clear 
	drop tax_paid* tent_cam* xcamt* camt_liab* 	
	gen ins_co = 0
		replace ins_co = 1 if sic == "6311"
		replace ins_co = 1 if sic == "6321"
		replace ins_co = 1 if sic == "6324"
		replace ins_co = 1 if sic == "6331"
		replace ins_co = 1 if sic == "6351"
		replace ins_co = 1 if sic == "6361"
		replace ins_co = 1 if sic == "6399"
		replace ins_co = 1 if sic == "6411"
	gen ins_gain = gliv if ins_co == 1 
		replace ins_gain = 0 if missing(ins_gain)
			
	forvalues i = 2013/2022 {
		* Calculate annual AFSI - Incorporate gains/losses for insurance co.'s 
		replace afsi_`i'_2 = afsi_`i'_2 - ins_gain
		replace afsi_`i'_2 = 0 if afsi_`i'_2 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_2 = fed_tax if fyear == `i' 
		replace tax_paid_`i'_2 = 0 if tax_paid_`i'_2 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_2 = afsi_`i'_2 * .15 
		gen xcamt_liab_`i'_2 = tent_camt_`i'_2 - tax_paid_`i'_2
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_2 = 0 if xcamt_liab_`i'_2 < 0
		egen camt_liab_`i'_2 = sum(xcamt_liab_`i'_2),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_2 = 0 if drop_`i' >0
	}
	drop test* app_* 
	* Applicable corporation test:
	replace afsi =  pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsi if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
			replace app_corp_`i' = 1 if app_`i' > 1000
			replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_2 = 0 if app_corp_`i' == 0 
		rename camt_liab_`i'_2 camt_liab_`i'_0 
	}	
	save temp, replace	
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_0 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_0 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_0 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		* This calculates aggregate CAMT liability in each year
		duplicates drop gvkey, force 
	forvalues i = 2013/2022 {
		egen total_camt_`i'_0 = sum(camt_liab_`i'_0)
		rename total_camt_`i'_0 camt_`i'
	}
		gen note = "09. Adjust for treatment of investment gains for insurance companies"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_0 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "09. Adjust for treatment of investment gains for insurance companies"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_0
		rename camt_liab_`i'_0 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "09. Adjust for treatment of investment gains for insurance companies"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_0 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "09. Adjust for treatment of investment gains for insurance companies"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_0 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 	
	
	
* Allow for loss carryforwards of AFSI beginning 2020:
	* Net operating losses and calculate AFSI
	use temp, clear
	xtset gvkeyid fyear
	foreach i in ni pi dp tax_dep gross_up_dta spectrum {
		replace `i' = 0 if missing(`i')
	}
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	* If there is an AFSI loss in 2012, it gets counted against 2013. If the 
	* difference is negative, this amount is carried forward as a loss into 
	* subsequent years.
	gen afsix = afsi 
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace afsi = afsi + L_`i' if fyear == `i'
	}	
	
	forvalues i = 2013/2022 {
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_3 = fed_tax if fyear == `i' 
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_3 = 0 if drop_`i' > 0 
	}	
	* Applicable corporation test (takes carryforward losses into account)
	drop test* app_* 
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsi if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 
	}
	save temp, replace
	
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
	forvalues i = 2013/2022 {
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "10. Allow for carryforward of AFSI losses"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "10. Allow for carryforward of AFSI losses"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "10. Allow for carryforward of AFSI losses"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "10. Allow for carryforward of AFSI losses"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 

* Limit the carryforward of losses to 80% of AFSI:
	* Net operating losses and calculate AFSI
	use temp, clear
	foreach i in ni pi txs dp tax_dep gross_up_dta spectrum {
		replace `i' = 0 if missing(`i')
	}
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	replace afsix = afsi
	* Calculate the limit on carryforward AFSI losses by multiplying 80% by 
	* current year AFSI 
	gen loss_limit = -1 * (.8 * afsi ) if afsi > 0
	* Create a variable to keep track of carryforward losses if the loss carry-
	* forward is limited.	
	gen loss_bank = 0 
	
	drop L_* afsi_* tax_paid* tent_camt_* xcamt_* camt_*  test* app_*  
* Original loss calculation
	gen afsi2 = afsi 
	gen afsi2x = afsi 
	forvalues i = 2013/2022 {
		* Calculate carryforward loss:
		gen L2_`i' = L.afsi2 if L.afsi2 < 0 & fyear == `i'
		replace L2_`i' = 0 if missing(L2_`i')
		replace afsi2 = afsi2 + L2_`i' if fyear == `i'
	}	
		
	xtset gvkeyid fyear
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace L_`i' = L_`i' + L.loss_bank if L.loss_bank != .
		gen l_limit_`i' = 0
		replace l_limit_`i' = 1 if loss_limit > L_`i' & fyear == `i' & afsi > 0
		replace afsi = afsi + L_`i' if fyear == `i' & l_limit_`i' == 0 
		* Apply 80% limitation on loss carryforward:
		* Calculate annual AFSI
			replace afsi = afsi * .2 if fyear == `i' & l_limit_`i' == 1 & afsi > 0 
			replace L_`i' = L_`i' - loss_limit if fyear == `i' & l_limit_`i' == 1
			replace loss_bank = L_`i' if fyear == `i' & l_limit_`i' == 1
	}
	
	forvalues i = 2013/2022 {
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_3 = fed_tax if fyear == `i' 
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_3 = 0 if drop_`i' >0
	}	
	* This is not used in the paper. It's a check on the impact that losses 
	* have on CAMT liability
	forvalues i = 2013/2022 {
		gen NL_afsi_`i' = afsi2 if fyear == `i'
		replace NL_afsi_`i' = 0 if missing(NL_afsi_`i')
		replace NL_afsi_`i' = 0 if NL_afsi_`i' < 0 
		gen NL_tax_paid_`i' = fed_tax if fyear == `i' 
		replace NL_tax_paid_`i' = 0 if NL_tax_paid_`i' < 0
		gen NL_tent_camt_`i' = NL_afsi_`i' * .15 
		gen NL_xcamt_liab_`i' = NL_tent_camt_`i' - NL_tax_paid_`i'
		replace NL_xcamt_liab_`i' = 0 if xcamt_liab_`i' < 0
		replace NL_xcamt_liab_`i' = 0 if fyear != `i'
		egen NL_camt_liab_`i' = sum(NL_xcamt_liab_`i'),by(gvkey)
		replace NL_camt_liab_`i' = 0 if drop_`i' >0
	}	
	* Applicable corporation test (takes losses into account)
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsi if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 
	}
	
	save temp, replace
	
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
	forvalues i = 2013/2022 {
		* This calculates aggregate CAMT liability in each year
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "11. 80% limitation on carryforward of losses"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "11. 80% limitation on carryforward of losses"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "11. 80% limitation on carryforward of losses"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "11. 80% limitation on carryforward of losses"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 	
* Disregard losses for purposes of applicable corporation status
	use temp, clear 
	foreach i in ni pi txs dp tax_dep gross_up_dta spectrum {
		replace `i' = 0 if missing(`i')
	}
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	* This is AFSI before consideration of loss carryforwards (used in app. corp test later)
	replace afsix = afsi
	drop L_* afsi_* tax_paid* tent_camt_* xcamt_* camt_*  test* app_* loss_limit l_* loss_bank
	gen loss_limit = -1 * (.8 * afsi ) if afsi > 0
	gen loss_bank = 0 
	
	xtset gvkeyid fyear
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace L_`i' = L_`i' + L.loss_bank if L.loss_bank != .
		gen l_limit_`i' = 0
		replace l_limit_`i' = 1 if loss_limit > L_`i' & fyear == `i' & afsi > 0
		replace afsi = afsi + L_`i' if fyear == `i' & l_limit_`i' == 0 
		* Apply 80% limitation on loss carryforward:
			replace afsi = afsi * .2 if fyear == `i' & l_limit_`i' == 1 & afsi > 0 
			replace L_`i' = L_`i' - loss_limit if fyear == `i' & l_limit_`i' == 1
			replace loss_bank = L_`i' if fyear == `i' & l_limit_`i' == 1
	}
	
	forvalues i = 2013/2022 {
		* Calculate annual ordinary corporate tax
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		gen tax_paid_`i'_3 = fed_tax if fyear == `i' 
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_3 = 0 if drop_`i' >0
	}	
	
	forvalues i = 2013/2022 {
		global x = `i' -4
		* We use AFSI before financial statement loss carryforwards:
		gen test_`i' = afsix if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 
	}	

	save temp, replace
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
	forvalues i = 2013/2022 {
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "12. Disregard adjustment for losses for determining app. corp status"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "12. Disregard adjustment for losses for determining app. corp status"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "12. Disregard adjustment for losses for determining app. corp status"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "12. Disregard adjustment for losses for determining app. corp status"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 		

* General business credits			
	use temp, clear 
* Estimate general business credits based on Hoopes and Kindt:
	replace credit_pct = abs(credit_pct)
	* Calculate the dollar value of impact of credits by multiplying by 
	* pretax income.
	gen etrcredits_pct = credit_pct * pi 
	replace etrcredits_pct = 0 if pi < 0
	replace etrcredits = etrcredits*-1 if etrcredits < 0
	replace etrcredits = 0 if missing(etrcredits)
	replace etrcredits_pct = 0 if missing(etrcredits_pct)
	* Note: firms report general business credits using either pct, dollar 
	* value, or both.
	gen credit = max(etrcredits, etrcredits_pct)
	
	* Calculate annual AFSI	
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	replace afsix = afsi
	drop L_* afsi_* tax_paid* tent_camt_* xcamt_* camt_*  test* app_* loss_limit l_* loss_bank
	gen loss_limit = -1 * (.8 * afsi ) if afsi > 0
	gen loss_bank = 0 
	
	xtset gvkeyid fyear
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace L_`i' = L_`i' + L.loss_bank if L.loss_bank != .
		gen l_limit_`i' = 0
		replace l_limit_`i' = 1 if loss_limit > L_`i' & fyear == `i' & afsi > 0
		replace afsi = afsi + L_`i' if fyear == `i' & l_limit_`i' == 0 
		* Apply 80% limitation on loss carryforward:
			replace afsi = afsi * .2 if fyear == `i' & l_limit_`i' == 1 & afsi > 0 
			replace L_`i' = L_`i' - loss_limit if fyear == `i' & l_limit_`i' == 1
			replace loss_bank = L_`i' if fyear == `i' & l_limit_`i' == 1
	}
	
	forvalues i = 2013/2022 {
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_3 = fed_tax + credit if fyear == `i' 
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.		
		replace camt_liab_`i'_3 = 0 if drop_`i' >0
	}	
	* Applicable corporation test:
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsix if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 
	}	
		
	save temp, replace
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
	forvalues i = 2013/2022 {
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "13. Allowance for general business credits"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "13. Allowance for general business credits"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "13. Allowance for general business credits"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "13. Allowance for general business credits"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
* General business credits limited to 75% of net income			
	use temp, clear 
	drop etrcredits_pct credit 
	* Please refer to prior section for an explanation
	replace credit_pct = abs(credit_pct)
	gen etrcredits_pct = credit_pct * pi 
	replace etrcredits_pct = 0 if pi < 0
	replace etrcredits = etrcredits*-1 if etrcredits < 0
	replace etrcredits = 0 if missing(etrcredits)
	replace etrcredits_pct = 0 if missing(etrcredits_pct)
	gen credit = max(etrcredits, etrcredits_pct)
	
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	* We now apply the limitation on general business credits:
	gen afsi_gbc = afsi 
		replace afsi_gbc = 0 if afsi_gbc < 0 
	gen gbc_limitation = .75 * (txfed + txdfed + afsi_gbc*.15) 
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txfed) if missing(gbc_limitation) & missing(txdfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txdfed) if missing(gbc_limitation) & missing(txfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15) if missing(gbc_limitation) & missing(txfed) & missing(txdfed)
		replace gbc_limitation = 0 if gbc_limitation < 0 
	* The credit is the lesser of the limit and the credit amount
	gen credit_limited = min(gbc_limitation, credit)

	* Calculate annual AFSI
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum 
	replace afsix = afsi
	drop L_* afsi_* tax_paid* tent_camt_* xcamt_* camt_*  test* app_* loss_limit l_* loss_bank
	gen loss_limit = -1* (.8 * afsi ) if afsi > 0
	gen loss_bank = 0 
	
	xtset gvkeyid fyear
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace L_`i' = L_`i' + L.loss_bank if L.loss_bank != .
		gen l_limit_`i' = 0
		replace l_limit_`i' = 1 if loss_limit > L_`i' & fyear == `i' & afsi > 0
		replace afsi = afsi + L_`i' if fyear == `i' & l_limit_`i' == 0 
		* Apply 80% limitation on loss carryforward:
			replace afsi = afsi * .2 if fyear == `i' & l_limit_`i' == 1 & afsi > 0 
			replace L_`i' = L_`i' - loss_limit if fyear == `i' & l_limit_`i' == 1
			replace loss_bank = L_`i' if fyear == `i' & l_limit_`i' == 1
	}
	
	forvalues i = 2013/2022 {
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_3 = fed_tax + credit_limited if fyear == `i' 
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_3 = 0 if drop_`i' >0
	}	
	* Applicable corporation test:
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsix if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 
	}	
		
	save temp, replace
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
	forvalues i = 2013/2022 {
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "14. Limitation on allowance for general business credits"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "14. Limitation on allowance for general business credits"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "14. Limitation on allowance for general business credits"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "14. Limitation on allowance for general business credits"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 		
	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 	
* The firm is an applicable corporation forever:
	use temp, clear 
	drop etrcredits_pct credit gbc_limitation credit_limited

	* See prior section (general business credits) for an explanation:
	replace credit_pct = abs(credit_pct)
	gen etrcredits_pct = credit_pct * pi 
	replace etrcredits_pct = 0 if pi < 0
	replace etrcredits = etrcredits*-1 if etrcredits < 0
	replace etrcredits = 0 if missing(etrcredits)
	replace etrcredits_pct = 0 if missing(etrcredits_pct)
	gen credit = max(etrcredits, etrcredits_pct)
	
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	gen afsi_gbc = afsi 
		replace afsi_gbc = 0 if afsi_gbc < 0 
	* See prior section (limitation on GBC) for explanation:
	gen gbc_limitation = .75 * (txfed + txdfed + afsi_gbc*.15) 
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txfed) if missing(gbc_limitation) & missing(txdfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txdfed) if missing(gbc_limitation) & missing(txfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15) if missing(gbc_limitation) & missing(txfed) & missing(txdfed)
		replace gbc_limitation = 0 if gbc_limitation < 0 
	gen credit_limited = min(gbc_limitation, credit)
	* Calculate annual AFSI
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	replace afsix = afsi
	drop L_* afsi_* tax_paid* tent_camt_* xcamt_* camt_*  test* app_* loss_limit l_* loss_bank
	gen loss_limit = -1* (.8 * afsi ) if afsi > 0
	gen loss_bank = 0 
	
	xtset gvkeyid fyear
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace L_`i' = L_`i' + L.loss_bank if L.loss_bank != .
		gen l_limit_`i' = 0
		replace l_limit_`i' = 1 if loss_limit > L_`i' & fyear == `i' & afsi > 0
		replace afsi = afsi + L_`i' if fyear == `i' & l_limit_`i' == 0 
		* Apply 80% limitation on loss carryforward:
			replace afsi = afsi * .2 if fyear == `i' & l_limit_`i' == 1 & afsi > 0 
			replace L_`i' = L_`i' - loss_limit if fyear == `i' & l_limit_`i' == 1
			replace loss_bank = L_`i' if fyear == `i' & l_limit_`i' == 1
	}
	forvalues i = 2013/2022 {
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		* Calculate annual ordinary corporate tax
		gen tax_paid_`i'_3 = fed_tax + credit_limited if fyear == `i' 
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_3 = 0 if drop_`i' >0
	}	
	
	* Applicable corporation test
	gen earliest_yr = .
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsix if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace earliest_yr = fyear * app_corp_`i' if fyear == `i'
		replace earliest_yr = . if earliest_yr == 0 
	}	
	* For each firm, find the earliest year that they are an applicable corp.
	egen first_app_yr = min(earliest_yr),by(gvkey)
		
	forvalues i = 2013/2022 {
		* If the year is after the earliest app corp year, the firm is an app. corp.
		replace app_corp_`i' = 1 if `i' >= first_app_yr & first_app_yr != . 
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 		
	}
			save temp, replace
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
	forvalues i = 2013/2022 {
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "15. Once an applicable corporation, always an applicable corporation"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "15. Once an applicable corporation, always an applicable corporation"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "15. Once an applicable corporation, always an applicable corporation"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "15. Once an applicable corporation, always an applicable corporation"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 

	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 

* CAMT Foreign Tax Credit:
	use temp, clear 
	drop etrcredits_pct credit gbc_limitation credit_limited earliest_yr first_app_yr
	* See GBC section for explanation:
	replace credit_pct = abs(credit_pct)
	gen etrcredits_pct = credit_pct * pi 
	replace etrcredits_pct = 0 if pi < 0
	replace etrcredits = etrcredits*-1 if etrcredits < 0
	replace etrcredits = 0 if missing(etrcredits)
	replace etrcredits_pct = 0 if missing(etrcredits_pct)
	gen credit = max(etrcredits, etrcredits_pct)
	
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	gen afsi_gbc = afsi 
		replace afsi_gbc = 0 if afsi_gbc < 0 
	* See GBC limit section for explanation:
	gen gbc_limitation = .75 * (txfed + txdfed + afsi_gbc*.15) 
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txfed) if missing(gbc_limitation) & missing(txdfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txdfed) if missing(gbc_limitation) & missing(txfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15) if missing(gbc_limitation) & missing(txfed) & missing(txdfed)
		replace gbc_limitation = 0 if gbc_limitation < 0 
	gen credit_limited = min(gbc_limitation, credit)

	* Estimate foreign tax credit based on current foreign tax expense:
	gen ftc = txfo 
		replace ftc = 0 if missing(ftc)
		replace ftc = 0 if ftc < 0 
	
	* Calculate annual AFSI
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum 
	replace afsix = afsi
	drop L_* afsi_* tax_paid* tent_camt_* xcamt_* camt_*  test* app_* loss_limit l_* loss_bank
	gen loss_limit = -1 * (.8 * afsi ) if afsi > 0
	gen loss_bank = 0 
	
	xtset gvkeyid fyear
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace L_`i' = L_`i' + L.loss_bank if L.loss_bank != .
		gen l_limit_`i' = 0
		replace l_limit_`i' = 1 if loss_limit > L_`i' & fyear == `i' & afsi > 0
		replace afsi = afsi + L_`i' if fyear == `i' & l_limit_`i' == 0 
		* Apply 80% limitation on loss carryforward:
			replace afsi = afsi * .2 if fyear == `i' & l_limit_`i' == 1 & afsi > 0 
			replace L_`i' = L_`i' - loss_limit if fyear == `i' & l_limit_`i' == 1
			replace loss_bank = L_`i' if fyear == `i' & l_limit_`i' == 1
	}
	forvalues i = 2013/2022 {
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		* Calculate annual ordinary corporate tax - incorporate GBC and FTC
		gen tax_paid_`i'_3 = fed_tax + credit_limited +ftc if fyear == `i' 
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.	
		replace camt_liab_`i'_3 = 0 if drop_`i' >0
	}	
	* Applicable corporation test
	gen earliest_yr = .
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsix if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace earliest_yr = fyear * app_corp_`i' if fyear == `i'
		replace earliest_yr = . if earliest_yr == 0 
	}	
	* Find the earliest year a firm is an applicable corporation
	egen first_app_yr = min(earliest_yr),by(gvkey)
		
	forvalues i = 2013/2022 {
		* Firm is an applicable corporation if the year follows the first year
		* they are an applicable corporation 
		replace app_corp_`i' = 1 if `i' >= first_app_yr & first_app_yr != . 
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 		
	}
			save temp, replace	
			
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3 > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
	forvalues i = 2013/2022 {
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "16. CAMT Foreign Tax Credit"
		keep note camt_* n_* etr_* cash_etr_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "16. CAMT Foreign Tax Credit"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "16. CAMT Foreign Tax Credit"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "16. CAMT Foreign Tax Credit"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 

	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 

* CAMT Foreign Tax Credit Limited to 15% of AFSI:
	use temp, clear 
	drop etrcredits_pct credit gbc_limitation credit_limited earliest_yr first_app_yr ftc 

	* See GBC section for explanation:
	replace credit_pct = abs(credit_pct)
	gen etrcredits_pct = credit_pct * pi 
	replace etrcredits_pct = 0 if pi < 0
	replace etrcredits = etrcredits*-1 if etrcredits < 0
	replace etrcredits = 0 if missing(etrcredits)
	replace etrcredits_pct = 0 if missing(etrcredits_pct)
	gen credit = max(etrcredits, etrcredits_pct)
	
	
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum 	
	* See GBC Limit section for explanation:
	gen afsi_gbc = afsi 
		replace afsi_gbc = 0 if afsi_gbc < 0 
	gen gbc_limitation = .75 * (txfed + txdfed + afsi_gbc*.15) 
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txfed) if missing(gbc_limitation) & missing(txdfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15 + txdfed) if missing(gbc_limitation) & missing(txfed)
		replace gbc_limitation = .75 * (afsi_gbc*.15) if missing(gbc_limitation) & missing(txfed) & missing(txdfed)
		replace gbc_limitation = 0 if gbc_limitation < 0 
	gen credit_limited = min(gbc_limitation, credit)
	* See FTC section for explanation:
	gen ftc = txfo 
		replace ftc = 0 if missing(ftc)
		replace ftc = 0 if ftc < 0 
	
	replace afsi = pi - txs + dp - tax_dep + gross_up_dta - spectrum - ins_gain
	replace afsix = afsi
	drop L_* afsi_* tax_paid* tent_camt_* xcamt_* camt_*  test* app_* loss_limit l_* loss_bank
	gen loss_limit = -1 * (.8 * afsi ) if afsi > 0
	gen loss_bank = 0 
	
	xtset gvkeyid fyear
	forvalues i = 2013/2022 {
		gen L_`i' = L.afsi if L.afsi < 0 & fyear == `i'
		replace L_`i' = 0 if missing(L_`i')
		replace L_`i' = L_`i' + L.loss_bank if L.loss_bank != .
		gen l_limit_`i' = 0
		replace l_limit_`i' = 1 if loss_limit > L_`i' & fyear == `i' & afsi > 0
		replace afsi = afsi + L_`i' if fyear == `i' & l_limit_`i' == 0 
		* Apply 80% limitation on loss carryforward:
			replace afsi = afsi * .2 if fyear == `i' & l_limit_`i' == 1 & afsi > 0 
			replace L_`i' = L_`i' - loss_limit if fyear == `i' & l_limit_`i' == 1
			replace loss_bank = L_`i' if fyear == `i' & l_limit_`i' == 1
	}
	forvalues i = 2013/2022 {
		gen afsi_`i'_3 = afsi if fyear == `i'
		replace afsi_`i'_3 = 0 if missing(afsi_`i'_3)
		replace afsi_`i'_3 = 0 if afsi_`i'_3 < 0 
		* Calculate FTC Limitation based on pretax foreign income:
		gen pifo_`i' = pifo if fyear == `i'
		replace pifo_`i' = 0 if missing(pifo_`i')
		replace pifo_`i' = 0 if pifo_`i' < 0 
		gen ftc_limit_`i' = 0 
		replace ftc_limit_`i' = .15 * pifo_`i' 
		* The FTC will be the minimum of the firms FTC and the limitation on FTC 
		gen ftc_`i' = min(ftc_limit_`i', ftc)
		gen tax_paid_`i'_3 = fed_tax + credit_limited +ftc_`i' if fyear == `i' 
		* Calculate annual ordinary corporate tax
		replace tax_paid_`i'_3 = 0 if tax_paid_`i'_3 < 0
		* Calculate tentative minimum tax by multiplying AFSI by 15%
		gen tent_camt_`i'_3 = afsi_`i'_3 * .15 
		* CAMT is the difference between CAMT and ordinary corporate tax
		gen xcamt_liab_`i'_3 = tent_camt_`i'_3 - tax_paid_`i'_3
		* If CAMT is less than zero, meaning federal tax exceeds tent. CAMT,
		* replace with zero
		replace xcamt_liab_`i'_3 = 0 if xcamt_liab_`i'_3 < 0
		replace xcamt_liab_`i'_3 = 0 if fyear != `i'
		egen camt_liab_`i'_3 = sum(xcamt_liab_`i'_3),by(gvkey)
		* Replace CAMT with zero if it is a non-US or non-C corp.
		replace camt_liab_`i'_3 = 0 if drop_`i' >0
	}	
	
	* Applicable corporation test
	gen earliest_yr = .
	forvalues i = 2013/2022 {
		global x = `i' -4
		gen test_`i' = afsix if fyear < `i' & fyear > $x
		egen app_`i' = mean(test_`i'),by(gvkey)
		gen app_corp_`i' = 0
		replace app_corp_`i' = 1 if app_`i' > 1000
		replace app_corp_`i' = 0 if missing(app_`i')
		replace earliest_yr = fyear * app_corp_`i' if fyear == `i'
		replace earliest_yr = . if earliest_yr == 0 
	}	
	egen first_app_yr = min(earliest_yr),by(gvkey)

	forvalues i = 2013/2022 {
		* Comment out the line below when determining N firms w/o consid. of perpetual membership rule
		* Table 3, panel A (Firms with CAMT (adj. app. corp. rule))
		replace app_corp_`i' = 1 if `i' >= first_app_yr & first_app_yr != . 
		replace camt_liab_`i'_3 = 0 if app_corp_`i' == 0 
		replace app_corp_`i' = 0 if xdrop_`i' == 1
	}

	forvalues i = 2013/2022 {
		gen applic_`i' = 1 if app_corp_`i' == 1 & fyear == `i'
		replace applic_`i' = 0 if missing(applic_`i')
	}
			save temp, replace	
	* Calculate stats for this:
		* This calculates the number of firms with CAMT liability in each year,
		* the average ETR for each firm with CAMT liability in that year 
		* and the average cash tax ETR for each firm with CAMT liability 
		* in that year.	
	use temp, clear
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
		egen n_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & camt_liab_`i'_3> 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & camt_liab_`i'_3 > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
		egen t_app_corp_`i' = sum(applic_`i')
	}
		duplicates drop gvkey, force 
		* This calculates aggregate CAMT liability in each year
	forvalues i = 2013/2022 {
		egen total_camt_`i'_3 = sum(camt_liab_`i'_3)
		rename total_camt_`i'_3 camt_`i'
	}
		gen note = "17. CAMT Foreign Tax Credit limited to 15% of foreign-source AFSI"
		keep note camt_* n_* etr_* cash_etr_* t_app_corp_*
		duplicates drop note, force 
		append using camt_est 
		sort note
		save camt_est, replace 
	* Find the firms which expect to pay the most & which firms drop on/off:
		* For 2022 and the reason why:
		use temp, clear
		keep if fyear == 2022 
		keep if camt_liab_2022_3 > 0 
		keep gvkey conm 
		merge 1:1 gvkey using L_2022 
		gen dropped = 1 if _merge == 2 
		gen added = 1 if _merge == 1 
		gen kept = 1 if _merge == 3
		foreach i in dropped added kept {
			replace `i' = 0 if missing(`i')
		}
		gen note = "17. CAMT Foreign Tax Credit limited to 15% of foreign-source AFSI"
		drop _merge 
		append using CAMT_2022 
		save CAMT_2022, replace
	forvalues i = 2013/2022 {
		* Which firms paid the most in year X
		use temp, clear
		duplicates drop gvkey, force
		gsort -camt_liab_`i'_3
		rename camt_liab_`i'_3 camt_liab_`i'
		gen rank = _n 
		keep if rank < 31
		keep conm camt_liab_`i' rank
		gen note = "17. CAMT Foreign Tax Credit limited to 15% of foreign-source AFSI"
		append using camt_firms_`i'
		save camt_firms_`i', replace 
	* Which firms dropped on/off in year X:
		use temp, clear
		keep if camt_liab_`i'_3 > 0
		keep if fyear == `i'
		keep gvkey conm		
		merge 1:1 gvkey using L_`i' 
		gen xdropped_`i' = 0
		replace xdropped_`i' = 1 if _merge == 2 
		egen dropped_`i' = sum(xdropped_`i')
		gen xadded_`i' = 0 
		replace xadded_`i' = 1 if _merge == 1
		egen added_`i' = sum(xadded_`i')
		gen note = "17. CAMT Foreign Tax Credit limited to 15% of foreign-source AFSI"
		duplicates drop note, force 
		keep note added_`i' dropped_`i' 
		merge 1:1 note using camt_est 
		drop _merge 
		save camt_est, replace 

	* Track which companies drop on/off:
		use temp, clear
		keep if camt_liab_`i'_3 > 0 & fyear == `i'
		keep gvkey conm 
		duplicates drop gvkey, force 
		save L_`i', replace 
	} 
	
* Calculate refunds of CAMT:
	use temp, clear 
	gen camt_ts = .	
	* Create a time series for firms' CAMT payments 
	forvalues i = 2013/2022{
		replace camt_ts = camt_liab_`i'_3 if fyear == `i'
	}
	* Calculate whether in a given year a firm would potentially receive a refund 
	* on CAMT - when ordinary tax exceeds CAMT 
	gen creditable_taxes = 0
	forvalues i = 2013/2022 {
		replace creditable_taxes = tax_paid_`i'_3 - tent_camt_`i'_3 if fyear == `i'
		replace creditable_taxes = 0 if creditable_taxes < 0  
		replace creditable_taxes = 0 if camt_liab_`i'_3 > 0 & fyear == `i'
	}
		
	replace camt_ts = 0 if missing(camt_ts)
	gen cum_camt = 0
	capture drop credit
	gen credit = 0 
	xtset gvkeyid fyear 
	sort gvkeyid fyear
	forvalues i = 2013/2022 {
		* Track payments and allow these to be reduced by potential refunds 
		replace cum_camt = camt_ts + L.cum_camt if fyear == `i' & L.cum_camt != .
		replace credit = min(cum_camt, creditable_taxes) if fyear == `i'
		gen refund_`i' = min(cum_camt, creditable_taxes) if fyear == `i'
		replace credit = 0 if fyear == `i' & camt_liab_`i'_3 > 0 
		replace refund_`i' = 0 if fyear == `i' & camt_liab_`i'_3 > 0 
		replace refund_`i' = 0 if missing(refund_`i')
		replace cum_camt = max(cum_camt - credit,0) 
	}

	save temp, replace 
	
	use temp, clear
* Calculate stats	
	forvalues i = 2013/2022 {
		gen x_`i' = 0
		replace x_`i' = 1 if refund_`i' > 0 & fyear == `i'
		egen rn_`i' = sum(x_`i')
		gen x_etr_`i' = etr if fyear == `i' & refund_`i' > 0
		gen x_cash_etr_`i' = cash_etr if fyear == `i' & refund_`i' > 0
		egen etr_`i' = mean(x_etr_`i')
		egen cash_etr_`i' = mean(x_cash_etr_`i')
	}
	forvalues i = 2013/2022 {
		egen total_refund_`i' = sum(refund_`i')
		gen n_ref_`i' = 1 if refund_`i' > 0 & refund_`i' != . 
		replace n_ref_`i' = 0 if missing(n_ref_`i')
		egen nr_`i' = sum(n_ref_`i')
	}
	duplicates drop gvkey, force 

		gen note = "18. CAMT Refunds"
		keep note total_refund_* rn_* nr_*
		duplicates drop note, force 
		append using camt_est 	
		save camt_est, replace

* Create table 2: CAMT Est. at every step
	use camt_est, clear
	drop n_2010 n_2011 n_2012 camt_2010 camt_2011 camt_2012 
	* This creates table 3, panel A (est. CAMT liab)
	order camt_201* camt_2020 camt_2021 camt_2022
	* This creates table 3, panel A (est. Refunds)
	order total_refund_201* total_refund_2020 total_refund_2021 total_refund_2022
	* This creates table 3, panel A (Number of firms with CAMT liabilities)
	order n_201* n_2020 n_2021 n_2022
	* This creates table 3, panel A (Number of firms with CAMT refunds)
	order nr_201* nr_2020 nr_2021 nr_2022
	* This creates talbe 3, panel A (Number of firms in-scope)
	order t_app_corp_201* t_app_corp_2020 t_app_corp_2021 t_app_corp_2022
	* Find the unique observations gives number of unique firms paying CAMT:
	use temp, clear 
	gen cnt_zeros = 0 
	forvalues i = 2013/2022 {
		replace cnt_zeros = 1 if camt_liab_`i'_3 > 0 & fyear == `i'
	}
	egen had_liab = sum(cnt_zeros),by(gvkeyid)
	keep if had_liab > 0 
	duplicates drop gvkeyid, force 
	* Find unique firms receiving refunds:
	use temp, clear 
	gen cnt_zeros = 0 
	forvalues i = 2013/2022 {
		replace cnt_zeros = 1 if refund_`i' > 0 & fyear == `i'
	}
	egen had_liab = sum(cnt_zeros),by(gvkeyid)
	keep if had_liab > 0 
	duplicates drop gvkeyid, force 	
* Create table 4: Names of companies with CAMT in 2022:
	use temp, clear 
	gsort -camt_liab_2022_3 
	*browse conm camt_liab_2022_3 pi if camt_liab_2022_3 > 0
	keep if fyear == 2022

